var sUserAgent = navigator.userAgent;
var isOpera = sUserAgent.indexOf("Opera") > -1;
var isIE = sUserAgent.indexOf("compatible") > -1 && sUserAgent.indexOf("MSIE") > -1 && !isOpera;
var isLtIE7 = false;
if (isIE) {
    var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
    reIE.test(sUserAgent);
    var fIEVersion = parseFloat(RegExp["$1"]);

    isLtIE7 = fIEVersion < 7.0;
}
var isKHTML = sUserAgent.indexOf('KHTML') > -1
				|| sUserAgent.indexOf('Konqueror') > -1
				|| sUserAgent.indexOf('AppleWebKit') > -1;
var isMoz = sUserAgent.indexOf('Gecko') > -1 && !isKHTML;
var isChrome = sUserAgent.indexOf('Chrome') > -1;

var Ssf = {	
	encodeURL : function(string) {
		try {
			return encodeURIComponent(string);
		} catch(e) {
			var str = escape(string);
			// escape don't encode +. but form replace  ' ' in fields by '+'
			return str.split('+').join('%2B');
		}
	},
	
	synSltAll: function(s) {
		var t = Element.up(s, 'table');
		var cs = Element.select(t, 'input.' + s.className);
		for(var i = 1, len = cs.length; i < len; i++) {
			if(!(cs[i].disabled || cs[i].readonly)) {
				cs[i].checked = s.checked;
			}
		}
	},
	
	cancel: function() {
		var url = window.location.href;
		if(url.indexOf('pid=') < 0) {
			if(url.indexOf('?') < 0) {
				url = url + '?pid=' + $('pid').value;
			} else {
				url = url + '&pid=' + $('pid').value;
			}
		}
		window.location.href = url;
	},
		
	MAX_ROWS: 100,
	markChanged: function(prefix, mid, ids) {
		for(var i = 0; i < this.MAX_ROWS; i++) {
			var b = true;
			var c = false;
			for(var j = 0, len = ids.length; j < len; j++) {
				var t = $(prefix + i + ids[j]);
				if(t == null) continue;
				b = false;
				
				var n = t.tagName.toUpperCase();
				if(n == 'INPUT' || n == 'TEXTAREA') {
					var v = t.value;
					if(v != null && !v.blank()) {
						c = true;
						break;
					}
				} else if(n == 'SELECT') {
					if(t.selectedIndex > 0) {
						c = true;
						break;
					}
				}
			}
		
			$(mid + i).value = c ? 'Y' : 'N';	
			if(b) return;
		}
	},
	
	POPUP_WND_NAME: "_AS_POPUP",
	open: function(url, opt) {
		opt = opt || {};
		var width  = opt.width || 640;
		var height = opt.height || 480;
		var left   = (screen.width  - width)/2;
		var top    = (screen.height - height)/2;
		var params = 'width=' + width + ', height=' + height;
		params += ', top='+top+', left='+left;
		params += ', directories=0';
		params += ', location=0';
		params += ', menubar=0';
		params += ', resizable=0';
		params += ', scrollbars=1';
		params += ', status=0';
		params += ', toolbar=0';
		return window.open(url, this.POPUP_WND_NAME, params);
	},
	
	print: function(url) {
		var _wnd = this.open(url, {width: 800, height: 600});
		if(isMoz) {
			Event.observe(_wnd, 'load', this._print.bindAsEventListener( {'wnd': _wnd}), false);
		}
	},
	
	_print : function( ) {
		try {
			if (!this.wnd || this.wnd.closed ) {
				return;
			}

			var err = this.wnd.document.getElementById( "birt_errorPage" );
			if( err && err.innerHTML != '' ) {
				return;
			}
			
			this.wnd.setTimeout( "window.print();", 1000);
		} catch (e) { }
	},
	
	getWinDims: function() {
		var x,y;
		if (self.innerHeight) {
			x = self.innerWidth;
			y = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) {
			x = document.documentElement.clientWidth;
			y = document.documentElement.clientHeight;
		} else if (document.body) {
			x = document.body.clientWidth;
			y = document.body.clientHeight;
		}
		return {width:x, height:y};
	},
	
	remove: function(t) {
		if(window.confirm(gMsg.remove)) {
			Element.next(t).click();
		}
	},
	
	confirm: function(t) {
		if(window.confirm(gMsg.confirm)) {
			Element.next(t).click();
		}
	},
	
	ErrMsg: {
		HOLDER_ID: 'errMsgBox',
		MASK_ID: 'ifErrMsgMask',
		
		show: function(f, _msg) {
			if(_msg) {
				$('errMsgTxt').innerHTML = _msg;
			} else {
				var msg = Element.next(f, '.error-msg');
				if(msg == null) {
					var _f = Element.up(f, '.decorate');
					msg = Element.down(_f, '.error-msg');
				}
				$('errMsgTxt').innerHTML = msg.innerHTML;
			}
			
			var t = $(this.HOLDER_ID);
			t.style.width = 'auto';
			
			var o = Position.cumulativeOffset(f);
			var w = Ssf.getWinDims();
			var d1 = Element.getDimensions(f);
			var d2 = Element.getDimensions(t);
			if(d2.width > 480) {
				t.style.width = '480px';
				d2.width = 480;
			}
			
			var c = f.parentNode;
			do {
				if(c.scrollLeft) { o[0] -= c.scrollLeft; }
				if(c.scrollTop) { o[1] -= c.scrollTop; }
		      	c = c.parentNode;
		    } while (c != document.body);
		    
		  	var isLeft = (o[0] + d1.width + d2.width) > w.width;
		  	var l1 = isLeft ? (o[0] - d2.width) : (o[0] + d1.width);
		  	var t1 = o[1];
		  			  	
		  	if(isLtIE7) {
		  		//l1 = isLeft ? l1 - 3 : l1 + 2;
		  		var m = $(this.MASK_ID);
		  		m.style.left = l1 + 'px';
		  		m.style.top = t1 + 'px';
		  		m.style.width = d2.width + 'px';
		  		m.style.height = d2.height + 'px';
		  		m.style.display = 'block';
		  	}		  	

			t.className = isLeft ? 'right' : 'left';
			t.style.left = l1 + 'px';
			t.style.top = t1 + 'px';
			t.style.visibility = 'visible';
		},
		
		hide: function() {
			if(isLtIE7) {
				Element.hide(this.MASK_ID);
			}	
			
			var t = $(this.HOLDER_ID);
			t.style.left = '-99999px';
			t.style.top = '-99999px';
			t.style.visibility = 'hidden';
		}
	},
	
	Tab: {
		show: function(t) {
			var s = Element.down(t, '.subtabs');
			var wt = Element.getWidth(t) + 1;
			var ws = Element.getWidth(s);
			if(wt > ws && !s.style.width) {
				s.style.width = wt + 'px';
			}
			
			var h = Element.getHeight(t) + 1;
			s.style.top = h + 'px';
			s.style.visibility = 'visible';			
		},
		
		hide: function(t) {
			var s = Element.down(t, '.subtabs');
			s.style.visibility = 'hidden';
		}
	}
}